package com.yxkj.railservicer.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yxkj.railservicer.R;
import com.yxkj.railservicer.bean.SongDaBean;
import com.yxkj.railservicer.bean.SubOrderDetailBean;
import com.yxkj.railservicer.pager.SongDa;
import com.yxkj.railservicer.util.AppUtils;
import com.yxkj.railservicer.util.ContentUtil;
import com.yxkj.railservicer.view.MyGridView;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sunyu on 2017/2/28.
 */

public class SongDaAdapter extends RecyclerView.Adapter<SongDaAdapter.MyViewHolder> {
    private Context context;
    private List<SongDaBean> list;
    private SongDa songDa;

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_songda_firstTime;
        TextView tv_songda_time;
        TextView tv_songda_lasttime;
        ImageView iv_songda_icon;
        TextView tv_songda_order;
        TextView tv_songda_name;
        TextView tv_songda_address_get;
        TextView tv_songda_address_post;
        Button btn_songda_tel;
        Button btn_songda_message;
        Button btn_songda;

        MyGridView gv_goods;
        TextView tv_total;

        ImageView iv_status;
        ImageView iv_cancel;
        TextView tv_status;
        TextView tv_cancel;
        TextView tv_songda_status;
        TextView tv_songda_cancel;
        Button btn_confirm;
        Button btn_cancel;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_songda_firstTime = (TextView) itemView.findViewById(R.id.tv_songda_firstTime);
            tv_songda_time = (TextView) itemView.findViewById(R.id.tv_songda_time);
            tv_songda_lasttime = (TextView) itemView.findViewById(R.id.tv_songda_lasttime);
            iv_songda_icon = (ImageView) itemView.findViewById(R.id.iv_songda_icon);
            tv_songda_order = (TextView) itemView.findViewById(R.id.tv_songda_order);
            tv_songda_name = (TextView) itemView.findViewById(R.id.tv_songda_name);
            tv_songda_address_get = (TextView) itemView.findViewById(R.id.tv_songda_address_get);
            tv_songda_address_post = (TextView) itemView.findViewById(R.id.tv_songda_address_post);

            btn_songda_tel = (Button) itemView.findViewById(R.id.btn_songda_tel);
            btn_songda_message = (Button) itemView.findViewById(R.id.btn_songda_message);
            btn_songda = (Button) itemView.findViewById(R.id.btn_songda);

            gv_goods = (MyGridView) itemView.findViewById(R.id.gv_goods);
            tv_total = (TextView) itemView.findViewById(R.id.tv_total);

            iv_status = (ImageView) itemView.findViewById(R.id.iv_status);
            iv_cancel = (ImageView) itemView.findViewById(R.id.iv_cancel);
            tv_status = (TextView) itemView.findViewById(R.id.tv_status);
            tv_cancel = (TextView) itemView.findViewById(R.id.tv_cancel);
            tv_songda_status = (TextView) itemView.findViewById(R.id.tv_songda_status);
            tv_songda_cancel = (TextView) itemView.findViewById(R.id.tv_songda_cancel);
            btn_confirm = (Button) itemView.findViewById(R.id.btn_confirm);
            btn_cancel = (Button) itemView.findViewById(R.id.btn_cancel);
        }
    }

    public SongDaAdapter(Context context, List<SongDaBean> list, SongDa songDa) {
        this.context = context;
        this.list = list;
        this.songDa = songDa;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_songda, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final SongDaBean songDaBean = list.get(position);
        holder.tv_songda_firstTime.setText(songDaBean.getFirst_time());
        holder.tv_songda_lasttime.setText(songDaBean.getLast_time());
        holder.tv_songda_time.setText(songDaBean.getTime());
        Picasso.with(context).load(songDaBean.getIcon()).into(holder.iv_songda_icon);
        holder.tv_songda_order.setText(songDaBean.getOrder());
        holder.tv_songda_name.setText(songDaBean.getName());
        holder.tv_songda_address_get.setText(songDaBean.getAddressGet());
        holder.tv_songda_address_post.setText(songDaBean.getAddressPost());

        List<SubOrderDetailBean> foodList = songDaBean.getFoodList();
//        String distributionCosts = songDaBean.getDistributionCosts();
//        if(distributionCosts != null){
//            BigDecimal price = new BigDecimal(distributionCosts);
//            foodList.add(new SubOrderDetailBean("配送费", 1, price));
//        }
        GridGoodsAdapter goodsAdapter = new GridGoodsAdapter(context, foodList);
        goodsAdapter.notifyDataSetChanged();
        holder.gv_goods.setAdapter(goodsAdapter);

        holder.tv_total.setText("￥" + songDaBean.getTotal());

        holder.tv_songda_cancel.setText(songDaBean.getCancelReason());

        holder.btn_songda.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                AlertDialog dialog = new AlertDialog.Builder(context)
//                        .setTitle("提示")
//                        .setMessage("确定已送达？")
//                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                songDa.deliveredGoods(position);
//                            }
//                        })
//                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//
//                            }
//                        })
//                        .show();

                songDa.dialog(position);
//                AppUtils.showMsgDialog(context, 4, new AppUtils.MsgCallBackData() {
//                    @Override
//                    public void clickMsg(Dialog d, String data) {
//                        Log.e("TAG", "===========" + data);
//                        if (data == null) {
//                            data = " ";
//                        }
//                        songDa.deliveredGoods(position, data);
//                    }
//                });

            }


        });

        if ("7055".equals(songDaBean.getOrderStatus())) {
            holder.btn_songda_message.setBackgroundResource(R.drawable.shape_white_change);
        } else if ("7050".equals(songDaBean.getOrderStatus())) {
            holder.btn_songda_message.setBackgroundResource(R.drawable.shape_white);
        } else {

        }

        if ("9010".equals(songDaBean.getOrderCancelStatus())) {
            holder.iv_status.setVisibility(View.VISIBLE);
            holder.iv_cancel.setVisibility(View.VISIBLE);
            holder.tv_status.setVisibility(View.VISIBLE);
            holder.tv_cancel.setVisibility(View.VISIBLE);
            holder.tv_songda_status.setVisibility(View.VISIBLE);
            holder.tv_songda_cancel.setVisibility(View.VISIBLE);
            holder.btn_confirm.setVisibility(View.VISIBLE);
            holder.btn_cancel.setVisibility(View.VISIBLE);

            holder.tv_songda_status.setText("用户申请取消订单");

            holder.btn_songda.setVisibility(View.GONE);
        } else {
            holder.iv_status.setVisibility(View.GONE);
            holder.iv_cancel.setVisibility(View.GONE);
            holder.tv_status.setVisibility(View.GONE);
            holder.tv_cancel.setVisibility(View.GONE);
            holder.tv_songda_status.setVisibility(View.GONE);
            holder.tv_songda_cancel.setVisibility(View.GONE);
            holder.btn_confirm.setVisibility(View.GONE);
            holder.btn_cancel.setVisibility(View.GONE);

            holder.btn_songda.setVisibility(View.VISIBLE);

        }
        holder.btn_songda_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(context)
                        .setTitle("提示")
                        .setMessage("确定发送短信？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                songDa.message(position);
                                holder.btn_songda_message.setBackgroundResource(R.drawable.shape_white_change);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();
            }
        });

        holder.btn_songda_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                songDa.phone(position);
            }
        });

        holder.btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                songDa.confirm(position);
            }
        });

        holder.btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(context, "拒绝", Toast.LENGTH_SHORT).show();
                AppUtils.showMsgDialog(context, new AppUtils.MsgCallBackData() {
                    @Override
                    public void clickMsg(Dialog d, String data) {
                        songDa.cancel(position, data);
                        d.dismiss();
                    }
                });

            }
        });

//        holder.btn_confirm.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context, "同意", Toast.LENGTH_SHORT).show();
//            }
//        });


    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
